package com.ht.persist.example;

import com.google.inject.*;
import com.google.inject.persist.PersistService;
import com.google.inject.persist.jpa.JpaPersistModule;
import com.ht.guice.querydsl.QuerydslModule;
import com.ht.persist.example.dao.TodoDao;
import com.ht.persist.example.dao.UsersDao;
import com.ht.persist.example.entity.Todo;
import com.ht.persist.example.entity.User;
import com.mysema.query.jpa.impl.JPAQuery;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * Created by bcw on 2014/4/10.
 */
public class Example {
    public static void main(String[] args) {
//        Injector injector = Guice.createInjector(new QuerydslModule("myJpaUnit"));
        Properties properties = new Properties();
        try {
            properties.load(Example.class.getResourceAsStream("/jpa.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        JpaPersistModule jpaPersistModule = new JpaPersistModule("myJpaUnit").properties(properties);
        Injector injector = Guice.createInjector(jpaPersistModule);

        PersistService service = injector.getInstance(PersistService.class);
        service.start();

        TodoDao todoDao = injector.getInstance(TodoDao.class);
        Todo todo = new Todo();
        todo.setSummary("something");
        todoDao.insertOrUpdate(todo);
//
        UsersDao userDao = injector.getInstance(UsersDao.class);
        User user = new User();
        user.setName("a8");
        user.setMaster(true);
        user.setBirthday(new Date());
        userDao.insertOrUpdate(user);
//
        List<User> users = userDao.findAll();
        for (User u : users) {
            System.out.println(u.getId() + ":" + u.getName() + "," + u.getBirthday() + "," + u.getMaster());
        }
//        System.out.println(userDao.findName(user.getId()));
//
        service.stop();
    }
}
